package com.hnly.djdl.model

import android.content.Context
import android.content.SharedPreferences
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

object Favors {
    private const val KEY_FAVORS = "Favors"
    private val favorsMap = hashMapOf<Long, com.hnly.djdl.model.Snack>()
    private var isInit = false
    private val _orderLines: MutableStateFlow<List<com.hnly.djdl.model.Snack>> = MutableStateFlow(
        com.hnly.djdl.model.Favors.getSnacks()
    )
    val orderLines: StateFlow<List<com.hnly.djdl.model.Snack>> get() = com.hnly.djdl.model.Favors._orderLines

    fun removeSnack(snackId: Long) {
        com.hnly.djdl.model.Favors._orderLines.value = com.hnly.djdl.model.Favors._orderLines.value.filter { it.id != snackId }
    }

    private fun getSp(context: Context): SharedPreferences? {
        return context.getSharedPreferences("Favors", Context.MODE_PRIVATE)
    }

    private fun splitString(str: String) {
        if (str.isNotEmpty()) {
            str.split(",").forEach { i ->
                runCatching {
                    val id = i.toLong()
                    com.hnly.djdl.model.indexSnackMap[id]?.let { snack ->
                        com.hnly.djdl.model.Favors.favorsMap.put(id, snack)
                    }
                }
            }
        }
    }

    fun checkInit(context: Context) {
        if (com.hnly.djdl.model.Favors.isInit.not()) {
            val str: String = com.hnly.djdl.model.Favors.getSp(context = context)
                ?.getString(com.hnly.djdl.model.Favors.KEY_FAVORS, "") ?: ""
            com.hnly.djdl.model.Favors.splitString(str)
            com.hnly.djdl.model.Favors._orderLines.value = com.hnly.djdl.model.Favors.getSnacks()
            com.hnly.djdl.model.Favors.isInit = true
        }
    }

    fun getSnacks(): List<com.hnly.djdl.model.Snack> {
        return com.hnly.djdl.model.Favors.favorsMap.values.toList()
    }

    fun contains(context: Context, id: Long): Boolean {
        com.hnly.djdl.model.Favors.checkInit(context = context)
        return com.hnly.djdl.model.Favors.favorsMap.containsKey(id)
    }

    fun put(context: Context, snack: com.hnly.djdl.model.Snack) {
        com.hnly.djdl.model.Favors.checkInit(context = context)
        com.hnly.djdl.model.Favors.favorsMap[snack.id] = snack
        com.hnly.djdl.model.Favors._orderLines.value = com.hnly.djdl.model.Favors.getSnacks()
        val joinToString: String = com.hnly.djdl.model.Favors.favorsMap.keys.joinToString(",")
        com.hnly.djdl.model.Favors.getSp(context = context)
            ?.edit()?.putString(com.hnly.djdl.model.Favors.KEY_FAVORS, joinToString)?.apply()
    }

    fun remove(context: Context, snack: com.hnly.djdl.model.Snack) {
        com.hnly.djdl.model.Favors.checkInit(context = context)
        com.hnly.djdl.model.Favors.favorsMap.remove(snack.id)
        com.hnly.djdl.model.Favors._orderLines.value = com.hnly.djdl.model.Favors.getSnacks()
        val joinToString: String = com.hnly.djdl.model.Favors.favorsMap.keys.joinToString(",")
        com.hnly.djdl.model.Favors.getSp(context = context)
            ?.edit()?.putString(com.hnly.djdl.model.Favors.KEY_FAVORS, joinToString)?.apply()
    }
}